package cn.kgc.layui.service.impl;

import cn.kgc.layui.dao.ManagerDao;
import cn.kgc.layui.dao.RoleDao;
import cn.kgc.layui.dao.impl.ManagerDaoImpl;
import cn.kgc.layui.dao.impl.RoleDaoImpl;
import cn.kgc.layui.entity.Manager;
import cn.kgc.layui.entity.Role;
import cn.kgc.layui.service.ManagerService;
import cn.kgc.layui.service.RoleService;
import cn.kgc.layui.util.LayUiResult;
import cn.kgc.layui.util.MD5;

import java.util.List;

/**
 * 学生物实现类
 * @author alex-ycp
 * @create 2021-07-16 15:43
 */
public class ManagerServiceImpl implements ManagerService {

    private ManagerDao managerDao=new ManagerDaoImpl();
    private RoleDao roleDao=new RoleDaoImpl();

    @Override
    public LayUiResult<Manager> findByPage(int start, int size, Manager manager) {

        List<Manager> managers = managerDao.findAllByPage(start, size,manager);
        for (Manager m:managers) {
            //todo 根据用户id查询其拥有的角色
            List<Role> list= roleDao.getRolesByManageId(m.getId()+"");
            m.setRoles(list);
        }
        long count = managerDao.selectTotalCount();

        LayUiResult<Manager> layUiResult = LayUiResult.<Manager>builder().code(0).msg("请求成功").count(count).data(managers).build();

        return layUiResult;
    }

    @Override
    public int save(Manager manager) {
        int result=0;
        try {
            manager.setPassWord(MD5.getEncryptPwd(manager.getPassWordView()));
            result=managerDao.save(manager);
            managerDao.saveManagerRole(result+"",manager.getRoleIds());
            System.out.println("----->result"+result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;


    }

    @Override
    public int update(Manager manager) {
        int result=0;
        try {
            result=managerDao.update(manager);
            managerDao.saveManagerRole(manager.getId()+"",manager.getRoleIds());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int delete(String id, String ids) {
        int result=0;
        try {
            if(null!=id&&id.length()>0){
                result=managerDao.delete(id);
            }else{
                String [] idArra= ids.split(",");
                for (String myId:idArra) {
                    result=managerDao.delete(myId);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return result;

    }
}
